package com.kedacom.freedomLabel.initdata;

import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.initdata.bean.CategoryNewBean;
import com.kedacom.freedomLabel.initdata.bean.EventBusMessage;
import com.kedacom.freedomLabel.initdata.dao.CategoryNewDao;
import com.kedacom.freedomLabel.initdata.json.CategoryJsonNewBean;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.lego.message.LegoMessageManager;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class InitCategoryNew {


    private static AppExecutors sExecutors;

    private static Gson gson = new GsonBuilder()
            .serializeNulls()
            .create();

    private static Handler sHandler = new Handler();

    public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static {
        sExecutors = AppExecutors.getInstance();
    }

    static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static void DeleteCategory() {
        sExecutors.queryIO().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AppDatabase.getInstance().getCategoryNewDao().delete();

                } catch (Exception e) {
                    Log.d("error==", e.toString());
                }
            }
        });


    }


    public static void TestInterfaceGetCategory(String urlBase, String devNum) {
        OkGo.<String>get(urlBase)
                .params("X-Time-Stamp", System.currentTimeMillis())
                .params("X-Dev-No", devNum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            CategoryJsonNewBean categoryJsonNewBean = gson.fromJson(response.body(),
                                    new TypeToken<CategoryJsonNewBean>() {
                                    }.getType());
                            if (categoryJsonNewBean.getStatus() == 200) {
                               LegoMessageManager.getInstance().sendMessage(Options.MSG_CONNECT_SUCCESS_TEST, new EventBusMessage(3, "服务器获取正常", "分类", 1));

                            } else {
                                LegoMessageManager.getInstance().sendMessage(Options.MSG_CONNECT_SUCCESS_TEST, new EventBusMessage(3, "服务器返回异常：" + categoryJsonNewBean.getMsg()+",不更新数据", "分类", 0));
                            }
                        } catch (Exception e) {
                            LegoMessageManager.getInstance().sendMessage(Options.MSG_CONNECT_SUCCESS_TEST, new EventBusMessage(3, "接口获取异常，不更新数据", "分类", 0));

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        LegoMessageManager.getInstance().sendMessage(Options.MSG_CONNECT_SUCCESS_TEST, new EventBusMessage(3, "接口不通", "分类", 0));
                    }
                });
    }



    public static void getCategoryData(String urlBase, String devNum) {
        OkGo.<String>get(urlBase)
                .params("X-Time-Stamp", System.currentTimeMillis())
                .params("X-Dev-No", devNum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            CategoryJsonNewBean categoryJsonNewBean = gson.fromJson(response.body(),
                                    new TypeToken<CategoryJsonNewBean>() {
                                    }.getType());
                            if (categoryJsonNewBean.getStatus() == 200) {

                                List<CategoryNewBean> categoryNewBeanList = new ArrayList<>();
                                categoryNewBeanList.clear();

                                if (categoryJsonNewBean.getData() != null) {

                                    if (categoryJsonNewBean.getData().getList().size() > 0) {

                                        sExecutors.queryIO().execute(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    CategoryNewDao categoryNewDao = AppDatabase.getInstance().getCategoryNewDao();
                                                    categoryNewDao.delete();
                                                } catch (Exception e) {
                                                    LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SHOW, new EventBusMessage(3, "数据库删除原始数据出错", "分类", 0));
                                                }

                                            }
                                        });


                                        for (com.kedacom.freedomLabel.initdata.json.DataBean.ListBean listBean : categoryJsonNewBean.getData().getList()) {
                                            CategoryNewBean categoryNewBean = new CategoryNewBean();
                                            categoryNewBean.setOtherId(listBean.getId());
                                            categoryNewBean.setName(listBean.getCate_name());
                                            categoryNewBean.setPrice(listBean.getPrice());
                                            categoryNewBeanList.add(categoryNewBean);
                                            if (listBean.getChildren().size() > 0) {
                                                for (com.kedacom.freedomLabel.initdata.json.DataBean.ListBean.ChildrenBean childrenBean : listBean.getChildren()) {
                                                    CategoryNewBean categoryNewBean01 = new CategoryNewBean();
                                                    categoryNewBean01.setOtherId(childrenBean.getId());
                                                    categoryNewBean01.setName(childrenBean.getCate_name());
                                                    categoryNewBean01.setPrice(childrenBean.getPrice());
                                                    categoryNewBeanList.add(categoryNewBean01);
                                                }
                                            }
                                        }

                                    }

                                    if (categoryNewBeanList.size() > 0) {
                                        sExecutors.queryIO().execute(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    CategoryNewDao categoryNewDao = AppDatabase.getInstance().getCategoryNewDao();
                                                    categoryNewDao.save(categoryNewBeanList);
                                                    LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SHOW, new EventBusMessage(3, "获取数据成功", "分类", 1));
                                                } catch (Exception e) {
                                                    LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SHOW, new EventBusMessage(3, "数据库出错", "分类", 0));
                                                    Log.d("error==", e.toString());
                                                }

                                            }
                                        });
                                    }

                                } else {
                                    LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SHOW, new EventBusMessage(3, "获取分类为空", "分类", 0));
                                }
                            } else {
                                LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SHOW, new EventBusMessage(3, "获取分类数据出错" + categoryJsonNewBean.getMsg(), "分类", 0));
                            }
                        } catch (Exception e) {
                            LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SHOW, new EventBusMessage(3, "获取分类数据异常", "分类", 0));

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SUCCESS, new EventBusMessage(3, "班次服务器出错", "分类", 0));
                    }
                });
    }
}
